/**
 * 主界面
 */

class MainScene extends BaseScene {

    img_bg: eui.Image;
    girlGroup: eui.Group;
    img_girl: eui.Image;
    img_zhong: eui.Image;
    img_fa: eui.Image;
    img_bai: eui.Image;

    ltGroup: eui.Group;
    head: HeadIcon;
    lab_uid: eui.Label;
    lab_nick: eui.Label;
    lab_money: eui.Label;
    btn_add: eui.Image;
    group_add: eui.Group;

    rtGroup: eui.Group;
    btn_notice: ButtonEx;
    tip_notice: eui.Image;
    btn_setting: ButtonEx;
    btn_rule: ButtonEx;

    lGroup: eui.Group;
    welfare: eui.Group;
    btn_welfare: ButtonEx;

    rGroup: eui.Group;
    btn_circle: ButtonEx;
    btn_create: ButtonEx;
    btn_join: ButtonEx;

    ldGroup: eui.Group;
    btn_daikai: ButtonEx;
    btn_moregames: ButtonEx;

    rdGroup: eui.Group;
    btn_realname: ButtonEx;
    btn_telphone: ButtonEx;
    btn_comment: ButtonEx;
    btn_record: ButtonEx;
    btn_recruit: ButtonEx;

    blink: egret.MovieClip;

    constructor() {
        super();
        this.skinName = "MainSceneSkin";
    }

    childrenCreated() {
        super.childrenCreated();

        GameMusic.play("music_scene");

        this.blink = FactoryUtils.createMovieclip('main_blink');
        this.blink.x = 228;
        this.blink.y = 238;
        this.girlGroup.addChild(this.blink);

        this.head.skinState = 'normal';

        this.img_bg.source = GameConfig.res_config['game_scene'];
        this.img_girl.source = GameConfig.res_config['main_other_girl'];
        this.btn_welfare.source = GameConfig.res_config['main_btn_welfare'];
        this.btn_circle.source = GameConfig.res_config['main_btn_laoyouquan'];
        this.btn_create.source = GameConfig.res_config['main_room_create'];
        this.btn_join.source = GameConfig.res_config['main_room_joinin'];

        this.animation();

        this.update();

        if (+game.roomid > 1000) {
            gameManager.socketManager.send(3, {
                roomid: +game.roomid,
                create_time: +game.roomCreateTime,
                pass: "0"
            });
        }

        game.user && Weixin.getLocation();

        if (+GameStorage.getData(GameStorage.first) != new Date().getUTCDate()) {
            GameStorage.setData(GameStorage.first, new Date().getUTCDate());

            if (GameConfig.switch_welfare) {
                gameManager.open('WelfarePanel');
            }
            else {
                if (!game.player.isVip && GameConfig.switch_shoujirenzheng) {
                    gameManager.open('TelphonePanel');
                }
                if (game.player.isVip) {
                    gameManager.open('NoticePanel');
                }
            }
        }

        this.updateConfig();
        /**老友圈 */
        Circle.parseCircleJoinRoom();
        if (Circle.args_2017 !== null) {
            Circle.args_2017.uid = game.player.uid;
            gameManager.socketManager.send(2017, Circle.args_2017);
        }

        this.btn_notice.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_setting.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_rule.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);

        this.btn_welfare.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);

        this.btn_circle.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_create.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_join.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);

        this.btn_daikai.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_moregames.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);

        this.btn_realname.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_telphone.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_comment.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_record.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_recruit.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);

        this.head.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_add.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.group_add.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);

        gameManager.i.addEventListener(GameEventType.REALNAME_CONFIRM, this.update, this);
        gameManager.i.addEventListener(GameEventType.TELPHONE_CONFIRM, this.update, this);

        gameManager.i.addEventListener(GameEventType.UPDATE_NOTICE, this.updateConfig, this);
    }

    private clickHandler(e: egret.TouchEvent) {
        switch (e.currentTarget) {
            case this.btn_notice:
                gameManager.open('NoticePanel');
                break;
            case this.btn_setting:
                gameManager.open('SettingPanel');
                break;
            case this.btn_rule:
                gameManager.open('RulePanel');
                break;
            case this.btn_welfare:
                gameManager.open('WelfarePanel');
                break;
            case this.btn_circle:
                GSDataProxy.i.sendCircleOpen();
                break;
            case this.btn_create:
                game.isDaikai = false;
                gameManager.open('CreatePanel');
                break;
            case this.btn_join:
                gameManager.open('JoininPanel');
                break;
            case this.btn_daikai:
                gameManager.open('DaikaiPanel');
                break;
            case this.btn_moregames:
                game.askPanel.showMsg(function (r) {
                    if (r) location.href = GameConfig.moregame_href + '?v=' + Math.random();
                }, '确定要离开当前游戏页面,\n\n前往游戏中心吗？', "前 往");
                break;
            case this.btn_realname:
                gameManager.open('RealnamePanel');
                break;
            case this.btn_telphone:
                gameManager.open('TelphonePanel');
                break;
            case this.btn_comment:
                game.isShowAsscess = true;
                gameManager.socketManager.send(28);
                break;
            case this.btn_record:
                gameManager.socketManager.send(19, {
                    start: 0,
                    end: GameConst.Count_Page_Record - 1,
                    time: 0
                });
                break;
            case this.btn_recruit:
                gameManager.open('RecruitPanel');
                break;

            case this.head:
                let infoPanel: RoleinfoPanel = gameManager.find('RoleinfoPanel');
                if (infoPanel) {
                    infoPanel.show();
                    infoPanel.refreshRole();
                }
                break;
            case this.group_add:
            case this.btn_add:
                gameManager.open('BuycardPanel');
                break;
        }
    }

    update(): void {

        this.head.update(game.player);

        this.lab_money.text = "" + StringUtils.getNumberStr(game.player.cur);
        this.lab_nick.text = "" + game.player.nick;
        this.lab_uid.text = "ID:" + game.player.uid;

        this.updateConfig();
    }

    private updateConfig() {
        //福利
        if (!GameConfig.switch_welfare && this.lGroup.contains(this.welfare)) {
            this.lGroup.removeChild(this.welfare);
        }

        //代开
        if (!GameConfig.switch_daikaifang && this.ldGroup.contains(this.btn_daikai)) {
            this.ldGroup.removeChild(this.btn_daikai);
        }
        //更多游戏
        if (!GameConfig.switch_moregames && this.ldGroup.contains(this.btn_moregames)) {
            this.ldGroup.removeChild(this.btn_moregames);
        }

        //实名认证
        if (game.player.isShiming && this.rdGroup.contains(this.btn_realname)) {
            this.rdGroup.removeChild(this.btn_realname);
        }
        //手机认证
        if ((!GameConfig.switch_shoujirenzheng || game.player.isVip) && this.rdGroup.contains(this.btn_telphone)) {
            this.rdGroup.removeChild(this.btn_telphone);
        }

        this.tip_notice.visible = false;
        let active: any;
        for (let i: number = 0; i < GameConfig.active_list.length; i++) {
            active = GameConfig.active_list[i];

            if (active.hasOwnProperty('tip')) {
                this.tip_notice.visible = true;
                return;
            }
        }
    }

    show() {
        super.show();

        this.blink.play(-1);
    }

    private animation() {
        EffectUtils.floating(this.img_zhong, 50, 2800);
        EffectUtils.floating(this.img_fa, 40, 1800);
        EffectUtils.floating(this.img_bai, 25, 1200);

        EffectUtils.shaking(this.btn_welfare, 20, 100, 4000);

        this.blink.play(-1);
    }
}